/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.managedbean;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import mrp.productionPlanning.entity.MPSTableEntity;
import mrp.productionPlanning.session.MPSTableSessionBean;
import org.primefaces.event.FlowEvent;

/**
 *
 * @author Wei Haoyang
 */
@ManagedBean
@ViewScoped
public class EditMPSTableManagedBean {

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger aLogger) {
        logger = aLogger;
    }

    private boolean skip;
    private static Logger logger  = Logger.getLogger(EditMPSTableManagedBean.class.getName());
    private List<MPSTableEntity> mpsListA = new ArrayList<MPSTableEntity>();
    private List<MPSTableEntity> mpsListB = new ArrayList<MPSTableEntity>();
    private List<MPSTableEntity> mpsListC = new ArrayList<MPSTableEntity>();
    private List<MPSTableEntity> mpsListD = new ArrayList<MPSTableEntity>();
    private String title;
    private int yearName;
    @EJB
    private MPSTableSessionBean mpsTableSessionBean = new MPSTableSessionBean();
    
    /** Creates a new instance of EditMPSTableManagedBean */
    public EditMPSTableManagedBean() {
    }
    
    public void initView(PhaseEvent event) {

        if (getMpsListA() == null) {
            setMpsListA((ArrayList<MPSTableEntity>) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("mpsListA"));
        }
        if (getMpsListB() == null) {
            setMpsListB((ArrayList<MPSTableEntity>) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("mpsListB"));
        }
        if (getMpsListC() == null) {
            setMpsListC((ArrayList<MPSTableEntity>) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("mpsListC"));
        }
        if (getMpsListD() == null) {
            setMpsListD((ArrayList<MPSTableEntity>) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("mpsListD"));
        }
        yearName = mpsListA.get(0).getMonth().getYear()+1900;
        setTitle("Sales Forecast for Year "+yearName);
    }
    
    public void save(ActionEvent event){
        for (int i = 0;  i< mpsListA.size(); i++){
            mpsTableSessionBean.saveMPSTable(mpsListA.get(i));
            mpsTableSessionBean.saveMPSTable(mpsListB.get(i));
            mpsTableSessionBean.saveMPSTable(mpsListC.get(i));
            mpsTableSessionBean.saveMPSTable(mpsListD.get(i));
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sales Forecast is Successfully Done.", ""));
    }
    
    public String onFlowProcess(FlowEvent event) {  
        logger.info("Current wizard step:" + event.getOldStep());  
        logger.info("Next step:" + event.getNewStep());  
          
        if(skip) {  
            skip = false;   //reset in case user goes back  
            return "confirm";  
        }  
        else {  
            return event.getNewStep();  
        }  
    }
    
    public boolean isSkip() {
        return skip;
    }

    public void setSkip(boolean skip) {
        this.skip = skip;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public MPSTableSessionBean getMpsTableSessionBean() {
        return mpsTableSessionBean;
    }

    public void setMpsTableSessionBean(MPSTableSessionBean mpsTableSessionBean) {
        this.mpsTableSessionBean = mpsTableSessionBean;
    }

    public List<MPSTableEntity> getMpsListA() {
        return mpsListA;
    }

    public void setMpsListA(List<MPSTableEntity> mpsListA) {
        this.mpsListA = mpsListA;
    }

    public List<MPSTableEntity> getMpsListB() {
        return mpsListB;
    }

    public void setMpsListB(List<MPSTableEntity> mpsListB) {
        this.mpsListB = mpsListB;
    }

    public List<MPSTableEntity> getMpsListC() {
        return mpsListC;
    }

    public void setMpsListC(List<MPSTableEntity> mpsListC) {
        this.mpsListC = mpsListC;
    }

    public List<MPSTableEntity> getMpsListD() {
        return mpsListD;
    }

    public void setMpsListD(List<MPSTableEntity> mpsListD) {
        this.mpsListD = mpsListD;
    }
}
